#!/usr/bin/perl -w

##HACKABOT_HELP##
# Flood IRC with ascii cows!
# !cowsay -l | [-f critter] something to say
##HACKABOT_HELP##

use strict;

my @allow = ( "bunny", "default", "koala", "small", "tux", "beaver", "owl", "shamrock", "clover", "sun");

my $default = `basename $0`;
chomp $default;

if ($default eq "cowsay") {
	$default = "default";
}
else {
	$default =~ s/say$//;
}

$ENV{'COWPATH'} = $ENV{'HB_ROOT'}."/misc/cows";

my $msg = <>;

if (not defined $msg or $msg =~ /^-h|--help/) {
	usage();
}
elsif ($msg =~ /^-l/) {
	print "send Critters:";
	foreach my $cow (@allow) {
		print " $cow";
	}
	print "\n";
}
elsif ($msg =~ /^-f\s*(\S*)\s+(\S+.*)/) {
	my $cow = $1;
	my $phrase = $2;
	if (grep(/^$cow$/, @allow)) {
		cowsay($phrase, $cow);
	}
	else {
		print "send Sorry, no $cow here.\n";
	}
}
else {
	cowsay($msg);
}


sub usage {
	print "send Usage: !cowsay -l OR !cowsay [-f critter] phrase...\n";
}

sub cowsay {
	my $msg = shift;
	my $cow = shift;
	if (not defined $cow) {
		$cow = $default;
	}
	print "sendnext\n";
	system("cowsay", "-f", $cow, "--", $msg);
}
